﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace 挂机锁
{
    public partial class Setting : Form
    {
        string pwd;
        int timeout, interval;
        public static Setting pnw;
        private int time;

        public int Time
        {
            get
            {
                time += 1; 
                return time; 
            }
            set { time = value; }
        }

        public int Interval
        {
            get { return interval; }
            set { interval = value*60*1000; }
        }

        public int Timeout
        {
            get { return timeout; }
            set { timeout = value; }
        }

        public string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }

        public Setting()
        {
            InitializeComponent();
            pnw = this;
        }

        
        private void Setting_Load(object sender, EventArgs e)
        {
            XmlDocument xmlfile = new XmlDocument();
            xmlfile.Load(@"config.xml");
            XmlNodeList elemList = xmlfile.GetElementsByTagName("pwd");
            for (int i = 0; i < elemList.Count; i++)
            {
                Pwd=elemList[i].InnerXml;
            }
            XmlNodeList elemList2 = xmlfile.GetElementsByTagName("timeout");
            for (int i = 0; i < elemList2.Count; i++)
            {
                Timeout =Convert.ToInt32(elemList2[i].InnerXml)*60*1000;
                txttime2.Text = elemList2[i].InnerXml;
            }
            XmlNodeList elemList3 = xmlfile.GetElementsByTagName("interval");
            for (int i = 0; i < elemList3.Count; i++)
            {
                Interval =Convert.ToInt32(elemList3[i].InnerXml);
                txttime1.Text = elemList3[i].InnerXml;
            }
            txtpwd.Text = Pwd;
            timer1.Interval = Interval;
            timer1.Stop();
            timer1.Start();
        }


        private void button1_Click_1(object sender, EventArgs e)
        {
            XmlDocument xmlfile = new XmlDocument();
            xmlfile.Load(@"config.xml");
            XmlNodeList elemList = xmlfile.GetElementsByTagName("pwd");
            for (int i = 0; i < elemList.Count; i++)
            {
                elemList[i].InnerXml = txtpwd.Text;
            }
            XmlNodeList elemList2 = xmlfile.GetElementsByTagName("timeout");
            for (int i = 0; i < elemList2.Count; i++)
            {
                elemList2[i].InnerXml =txttime2.Text;
            }
            XmlNodeList elemList3 = xmlfile.GetElementsByTagName("interval");
            for (int i = 0; i < elemList3.Count; i++)
            {
                elemList3[i].InnerXml = txttime1.Text;
            }
            timer1.Interval =Convert.ToInt32(txttime1.Text)*60*1000;
            xmlfile.Save(@"config.xml");
            timer1.Stop();
            timer1.Start();
            timer2.Stop();
            time = 0;
            timer2.Start();
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = false;
            this.Hide();
        }

        private void Setting_FormClosing(object sender, FormClosingEventArgs e)
        {
            notifyIcon1.Dispose();
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.ShowInTaskbar = true;
            this.BringToFront(); 
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            LOCK lookscreen = new LOCK();
            lookscreen.Show();
            lookscreen.BringToFront();
            
        }

        private void lineShape1_Click(object sender, EventArgs e)
        {

        }

        private void Setting_Deactivate(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                this.Hide();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            notifyIcon1.Text = Time.ToString();
        }

        private void txttime1_TextChanged(object sender, EventArgs e)
        {
            Regex a = new Regex("[^0-9]");
            if (a.IsMatch(txttime1.Text))
            {
                MessageBox.Show("只能是数字");
                txttime1.Text = "45";
            }
        }

        private void txttime2_TextChanged(object sender, EventArgs e)
        {
            Regex a = new Regex("[^0-9]");
            if (a.IsMatch(txttime2.Text))
            {
                MessageBox.Show("只能是数字");
                txttime2.Text = "10";
            }
        }

        private void txtpwd_TextChanged(object sender, EventArgs e)
        {
            Regex a = new Regex("[^0-9a-zA-Z]");
            if (a.IsMatch(txtpwd.Text))
            {
                MessageBox.Show("只能是数字或字母");
                txtpwd.Text = "123";
            }
        }





    }
}
